package org.exaple.laixe;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Result extends Activity {
	
	private ArrayList<Integer> Questions = new ArrayList<Integer>();// Store 30 questions
	private ArrayList<Integer> Answers = new ArrayList<Integer>();// store 30 answers
	private ArrayList<Integer> Corrects = new ArrayList<Integer>();// store 30 correct answer(1-4)
	private Long time;
	
	TextView h1;
	TextView h2;
	TextView h3;
	TextView h4;

	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.result);
       
        // get Extra information from Thi class
        Bundle b = getIntent().getExtras();
        Questions = b.getIntegerArrayList("Questions");
        Answers = b.getIntegerArrayList("Answers");
        Corrects = b.getIntegerArrayList("Corrects");
        time = b.getLong("time");
        
        System.out.println(Questions.toString());
        System.out.println(Answers.toString());
        System.out.println(Corrects.toString());
        
        
        h1 = (TextView) findViewById(R.id.result_h1);
        h2 = (TextView) findViewById(R.id.result_h2);
        h3 = (TextView) findViewById(R.id.result_h3);
        h4 = (TextView) findViewById(R.id.result_h4);
        
        String h1_string = getResources().getString(R.string.result_h1).replace("{0}", String.valueOf(CountCorrectAnswer()) )
        																.replace("{1}", "30");
        String h2_string = getResources().getString(R.string.result_h2).replace( "{0}", hang());
        String h3_string = getResources().getString(R.string.result_h3).replace("{0}", String.valueOf((1200 - time ) ) );
        String h4_string = getResources().getString(R.string.result_h4).replace("{0}", KQ());
        
        h1.setText(h1_string);
        h2.setText(h2_string);
        h3.setText(h3_string);
        h4.setText(h4_string);
        
        ((Button) findViewById(R.id.result_btnXemDapAn)).setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {				
				Intent resultReview = new Intent();
				Bundle b = new Bundle();
				b.putIntegerArrayList("Answers", Answers);
				b.putIntegerArrayList("Questions", Questions);
				b.putIntegerArrayList("Corrects", Corrects);	
				b.putLong("time", time);
				resultReview.setClass(getBaseContext(), ResultAnswersReview.class);
				resultReview.putExtras(b);
				startActivity(resultReview);
				finish();
			}
		});
        ((Button) findViewById(R.id.result_btnDong)).setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {				
				finish();
			}
		});
	}
	
	public int CountCorrectAnswer() {
		int count = 0;
		for (int i = 0; i < 30; i++) {
			if(Answers.get(i) == Corrects.get(i))
				count++;
		}
		return count;
	}
	
	public String hang() {
		if (Laixe.hang == "B1")
			return "26".toString();
		 
		return "28".toString();
		
	}
	
	public String KQ() {		
		if(Laixe.hang == "B1" && CountCorrectAnswer() >= 26 ) {
			return getResources().getString(R.string.result_passed);
		}
		if(Laixe.hang == "B2" && CountCorrectAnswer() >= 28 ) {
			return getResources().getString(R.string.result_passed);
		}
		return getResources().getString(R.string.result_failed);
	}
	
	

}

